//
//  KnowViewController.swift
//  sinofert_Swift
//
//  Created by enway-wk on 2017/6/23.
//  Copyright © 2017年 enway-wk. All rights reserved.
//

import UIKit
import MJRefresh
import YYModel

class KnowViewController: BaseViewController ,UITableViewDelegate,UITableViewDataSource{
    
    var tableView:UITableView!
    lazy var dataArray = Array<Any>()
    var page:Int = 1
    override func viewDidLoad() {
        super.viewDidLoad()
        
        requestData()
        
        self.view.backgroundColor = UIColor.white
        tableView = UITableView()
        tableView.frame = CGRect(x: 0.0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(tableView)
        
        tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(knowRefresh))
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(knowLoading))
    }
    func knowRefresh(){
        self.page = 1
        self.dataArray.removeAll()
        requestData()
        self.tableView.reloadData()
    }
    
    func knowLoading(){
        self.page += 1
        requestData()
        tableView.mj_footer.endRefreshing()
    }
    
    func requestData(){
        
        let url = "\(SERVER_ADDR)/zhnyxxgc/httpservice.action/searchQuestions"
        let parms = ["pageNum":self.page,"pageSize":10]
        HTTPService.shared.request(method: .POST, URLString: url, parameters: parms as [String : AnyObject]) { (json, isSuccess) in
            if json != nil{
                let dict = json as AnyObject
                let status = dict.value(forKey: "resultcode") as? String
                var arrDic = [[String:Any]]()
                arrDic = (dict.value(forKey: "resultdata") as? Array)!
                if status == "ok"{
                    print(arrDic.count)
                    print(dict["resultdata"])
                    for dic:Dictionary in arrDic{
                        let knowM = KnowModel()
                        knowM.yy_modelSet(with: dic)
                        self.dataArray.append(knowM)
                    }
                }
                self.tableView.reloadData()
                self.tableView.mj_header.endRefreshing()
            }
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        let cellID = "CELLID"
        var cell: KnowTableViewCell?
        cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? KnowTableViewCell
        if cell == nil {
            cell = KnowTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellID)
        }
        cell?.model = dataArray[indexPath.row] as! KnowModel
        return cell!
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
